package com.example.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Vip;
import com.example.springboot.service.VipService;

@CrossOrigin
@RestController

@RequestMapping("/vip")
public class VipController {
    @Autowired
    VipService vipService;
    
    
    @PostMapping("/add")
    public Result add(@RequestBody Vip vip){
        try{
            vipService.save(vip);
        }catch(Exception e){
            e.printStackTrace();
            return Result.error("插入数据错误");
        }
        return Result.success();
    }
    
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Vip> vipList=vipService.list();
        return Result.success(vipList);
    }
    @GetMapping("/selectById/{orderid}")
        public Result selectById(@PathVariable String orderid){
        Vip vip=vipService.getById(orderid);
        return Result.success(vip);
    }

    @GetMapping("/selectByTime/{time}")
    public Result selectByTime(@PathVariable String time){
        QueryWrapper<Vip> queryWrapper = new QueryWrapper<>();
        // 使用 leftLike 方法来匹配年月开头的部分
        queryWrapper.like("time", time);
        List<Vip> list4= vipService.list(queryWrapper);
        return Result.success(list4);
    }
    
}
